<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirm-loading="confirmLoading"
    cancel-text="关闭"
    @ok="submit"
    @cancel="closeModal"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="部门名称"
        >
          <a-input
            v-decorator="['name', validatorRules.name ]"
            placeholder="请输入部门名称"
          />
        </a-form-item>
        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="部门编号"
        >
          <a-input
            v-decorator="['code', validatorRules.code ]"
            placeholder="请输入部门编号"
          />
        </a-form-item>
        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="部门排序"
        >
          <a-input-number
            v-decorator="['sort', validatorRules.sort ]"
            placeholder="请输入部门排序"
            :min="1"
            :max="999"
            style="width: 200px"
          />
        </a-form-item>
        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="顶级部门"
        >
          <a-radio-group
            name="radioGroup"
            v-decorator="['isTop', validatorRules.isTop]"
            @change="funDeptShow"
          >
            <a-radio :value="1">是</a-radio>
            <a-radio :value="0">否</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          v-show="deptShow"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="上级部门"
        >
          <treeselect
            v-model="model.pid"
            :loadOptions="loadDepts"
            :options="depts"
            style="width: 370px;"
            placeholder="选择上级类目"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect';
import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect';

import ApiTree from '@/api/system/dept';
import { Slime } from '@/mixins/slime';
export default {
  name: 'Dept',
  components: { Treeselect },
  data() {
    return {
      title: '编辑',
      type: 0, // 0 添加 1 编辑
      confirmLoading: false,
      form: this.$form.createForm(this),
      model:{
        id: null,
        pid: 0,
      },
      deptShow: false,
      depts: [],
      validatorRules: {
        name: { rules: [{ required: true, message: '请输入商品类型名称!' }] },
        code: { rules: [{ required: true, message: '请输入商品部门编号!' }] },
        sort: { rules: [{ required: true, message: '请输入商品部门排序!' }] },
        isTop: {
          rules: [{ required: true, message: '请输入商品部门排序! ' }],
          initialValue: 1,
        },
      },
    };
  },
  mixins: [Slime],
  methods: {
    // 增
    crudCreate() {
      this.visible = true;
      this.type = 0;
      this.getDepts();
    },
    // 改
    crudUpdate(data) {
      this.visible = true;
      this.type = 1;
      this.getDepts();
      this.$nextTick(() => {
        this.form.setFieldsValue({
          name: data.name,
          code: data.code,
          sort: data.sort,
          isTop: data.pid === 0 ? 1 : 0,
        });
      });
      this.model = {
        id: data.id,
        pid: data.pid,
      }
      deptShow: data.pid === 0 ? false : true;
    },
    // 弹窗-是否顶级部门显示
    funDeptShow(e) {
      if (e.target.value == 1) {
        this.deptShow = false;
      } else {
        this.deptShow = true;
      }
    },
    // 弹窗-获取部门信息
    getDepts() {
      ApiTree.getDeptTree(0).then((res) => {
        this.depts = res.result.map(function (obj) {
          obj = { ...obj, label: obj.name };
          if (obj.subCount > 0) {
            obj.children = null;
          }
          return obj;
        });
      });
    },
    // 弹窗-获取下一级的部门菜单
    getSupDepts(id) {
      ApiTree.getDeptSuperior(id).then((res) => {
        const data = res.result;
        const children = data.map(function (obj) {
          if (!obj.leaf && !obj.children) {
            obj.children = null;
          }
          obj = { ...obj, label: obj.name };
          return obj;
        });
        this.depts = [{ id: 0, label: '顶级类目', children: children }];
      });
    },
    // 弹窗-树状图内部门数据
    loadDepts({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        ApiTree.getDeptTree(parentNode.id).then((res) => {
          parentNode.children = res.result.map(function (obj) {
            if (obj.subCount > 0) {
              obj.children = null;
            }
            obj = { ...obj, label: obj.name };
            return obj;
          });
          setTimeout(() => {
            callback();
          }, 100);
        });
      }
    },
    // 提交
    submit() {
      const that = this;
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          const param = {...values, ...that.model};
          if (that.type === 0) {
            that.submitCreate(param);
          } else {
            that.submitUpdate(param);
          }
        }
      });
    },
    // 提交 增
    submitCreate(param) {
      const that = this;
      ApiTree
        .create(param)
        .then((res) => {
          that.$message.success('新增成功');
        })
        .finally(() => {
          that.closeModal();
        });
    },
    // 提交 改
    submitUpdate(param) {
      const that = this;
      ApiTree
        .update(param)
        .then((res) => {
          that.$message.success('编辑成功');
        })
        .finally(() => {
          that.closeModal();
        });
    },
    // 弹窗-关闭
    closeModal() {
      this.confirmLoading = false;
      this.visible = false;
      this.form.resetFields()
      this.$emit('closeModal');
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>